/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment_cp3120.view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

/**
 * This is the main frame for the GUI. It works by allowing content to be
 * added to the center of a border layout via the setContents function.
 * @author Richard from Naomi's StartupGUI
 */
public class MainFrame extends JFrame{
    private JPanel titlePanel;
    private JPanel bottomPanel;
    private JPanel leftSidePanel;
    private JPanel rightSidePanel;
    
    private JPanel titleFlowPanel;
    private JPanel subTitleFlowPanel;
    
    private JPanel contentsPanel;
    
    private JLabel title;
    private Font titleFont;
    private JLabel subTitle;
    
    public static final int EXIT_TYPE = DO_NOTHING_ON_CLOSE;
    
    public static final int FRAME_WIDTH = 800;
    public static final int FRAME_HEIGHT = 670;
    
    public static final int SIDE_PANEL_RATIO = 8;
    
    public static final int TITLE_PANEL_WIDTH = FRAME_WIDTH;
    public static final int TITLE_PANEL_HEIGHT = FRAME_HEIGHT/8;
    
    public static final int CENTER_PANE_WIDTH = FRAME_WIDTH - FRAME_WIDTH/SIDE_PANEL_RATIO*2;
    public static final int CENTER_PANE_HEIGHT = FRAME_HEIGHT - FRAME_HEIGHT/2;
    /**
     * Defualt constructer. Calls the other constructor with JCU Airlines as its input.
     */
    public MainFrame() {
        this("JCU Airlines");
    }
    /**
     * MainFrame constructor. Sets the initail view the frame and sets the title of the frame to the input string
     * @param frameName
     * Name of the frame to be set
     */
    public MainFrame(String frameName) {
        //Create the welcome title
        setTitle(frameName);
        titlePanel = new JPanel(new GridLayout(0,1));                              //Create a panel to show the welcome header text
        titleFlowPanel = new JPanel(new FlowLayout());
        subTitleFlowPanel = new JPanel(new FlowLayout());
        title = new JLabel("Welcome to JCU Airlines");          //Create the title
        titleFont = new Font("serif", Font.BOLD, 28);           //Set the titleFont properties
        title.setFont(titleFont);                               //Set the font properties to the title
        title.setAlignmentY(Component.CENTER_ALIGNMENT);        //Vertically center the title in the panel
        subTitle = new JLabel();
        titleFont = new Font("serif", Font.BOLD, 24);
   
        titleFlowPanel.add(title);                                  //Add the title to the panel
        subTitleFlowPanel.add(subTitle);        
        
        titlePanel.add(titleFlowPanel);
        titlePanel.add(subTitleFlowPanel);
        
        titlePanel.setPreferredSize(new Dimension(TITLE_PANEL_WIDTH, TITLE_PANEL_HEIGHT));    //Set the size of the panel
        titlePanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
        add(titlePanel, BorderLayout.NORTH);             //Add the panel to the top of the window

        bottomPanel = new JPanel();
        bottomPanel.setPreferredSize(new Dimension(TITLE_PANEL_WIDTH, TITLE_PANEL_HEIGHT));
        bottomPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
        
        add(bottomPanel, BorderLayout.SOUTH);
        
        //Set the window properties
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //Set default close option
        setSize(FRAME_WIDTH, FRAME_HEIGHT);                //Set the size of the window
        
        leftSidePanel = new JPanel();
        leftSidePanel.setPreferredSize(new Dimension(this.getWidth()/SIDE_PANEL_RATIO, FRAME_HEIGHT));
        
        rightSidePanel = new JPanel();
        rightSidePanel.setPreferredSize(leftSidePanel.getPreferredSize());

        contentsPanel = new JPanel();
        
        add(leftSidePanel, BorderLayout.WEST);
        add(rightSidePanel, BorderLayout.EAST);
        add(contentsPanel, BorderLayout.CENTER);
        //center frame
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_TYPE);
        super.setResizable(false);
        
        addWindowListener(new WindowAdapter(){
            @Override public void windowClosing(WindowEvent e){
                if(quitMessageBox()){
                    System.exit(0);
                }   
            }
        });
        
    }

    /**
     * Override the setResizable to do nothing so that the frame can never be resized
     * @param resizable
     */
    @Override public void setResizable(boolean resizable) {
        
    }
    /**
     * Get the current contents panel
     * @return
     * Returns the contentsPanel
     */
    public JPanel getContents(){
        return contentsPanel;
    }
    /**
     * Set the contentsPanel to the panel passed
     * @param panel
     * panel that will become the contents
     */
    public void setContents(JPanel panel){ 
        clearContents();
        if(panel == null){
            panel = new JPanel();
        }
        contentsPanel = panel;
        contentsPanel.setMinimumSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT));
        contentsPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));        
        
        add(contentsPanel, BorderLayout.CENTER); 
        repaint();
        validate();
    }
    /**
     * Clears the current contents
     */
    public void clearContents(){
        remove(contentsPanel);
    }
    /**
     * Returns the current subTitle.
     * @return
     * Returns the subTitle lable
     */
    public JLabel getSubTitle(){
        return subTitle;
    }
    /**
     * Sets the subTitle to the input lable 
     * @param subTitle
     * Lable to set the subTitle to
     */
    public void setSubTitle(String subTitle){       
        subTitleFlowPanel.remove(this.subTitle);
        this.subTitle = new JLabel(subTitle);
        this.subTitle.setFont(titleFont);
        this.subTitle.setAlignmentY(Component.CENTER_ALIGNMENT);
        subTitleFlowPanel.add(this.subTitle);
        titlePanel.remove(subTitleFlowPanel);
        titlePanel.add(subTitleFlowPanel);
        validate();
        repaint();
    }
    /**
     * Sets the frame to visible
     */
    public void showGUI(){
        setVisible(true);
    }
    /**
     * Sets the frame to invisible
     */
    public void hideGUI(){
        setVisible(false);
    }
    /**
     * A function to create a message dialog box
     * @param parentcomponent 
     * Frame to display dialog in
     * @param message
     * Message to be displayed
     * @param title
     * Message title
     * @param messageType
     * Message Type
     */
    public void message(Component parentcomponent,
            Object message,
            String title,
            int messageType ){            
        JOptionPane.showMessageDialog(parentcomponent,message,title,messageType);
    }
    /**
     * A yes/no dialog for the quit message. It is use to confirm that the user wants to quit
     * @return
     * Returns slected option
     */
    public boolean quitMessageBox(){
        int selected = JOptionPane.showConfirmDialog(
                            this,
                            "Are you sure you want to quit?",
                            "Exit?",
                            JOptionPane.YES_NO_OPTION);

        if(selected == JOptionPane.YES_OPTION){
            return true;
        }
        return false;
    }
    /**
     * A message to display an error message
     * @param message
     * Message to be displayed
     * @param title
     * Title of the message box
     */
    public void errorMessageBox(String message, String title){
        message(this,message,
        title,
        JOptionPane.ERROR_MESSAGE);
    }
}
